#include "stdafx.h"
#include "include/Exceptions.h"

namespace Gs
{
	// Initialises a new instance of the Exception class.
	Exception::Exception()
		: _message("")
	{
	}

	// Initialises a new instance of the Exception class.
	Exception::Exception(const char *format, ...)
	{
		va_list args;
		va_start(args, format);
		char buffer[256];
		vsprintf_s(buffer, 256, format, args);
		_message = buffer;
	}

	// Gets the message of the exception.
	const char *Exception::GetErrorMessage()
	{
		return _message.c_str();
	}

	// Initialises a new instance of the InvalidConversionException class.
	InvalidConversionException::InvalidConversionException(const char *from, const char *to)
		: Exception("Could not convert from '%s' to '%s'.", from, to)
	{
	}

	// Initialises a new instance of the InvalidParameterException class.
	InvalidParameterException::InvalidParameterException(const char *param, const char *expected)
		: Exception("Invalid parameter '%s'. Expected %s.", param, expected)
	{
	}

	// Initialises a new instance of the InvalidOperationException class.
	InvalidOperationException::InvalidOperationException(const char *reason)
		: Exception(reason)
	{
	}

	// Initialises a new instance of the IndexOutOfBoundsException class.
	IndexOutOfBoundsException::IndexOutOfBoundsException()
		: Exception("Index out of bounds.")
	{
	}
		
	// Initialises a new instance of the IndexOutOfBoundsException class.
	IndexOutOfBoundsException::IndexOutOfBoundsException(const char *reason)
		: Exception(reason)
	{
	}

	// Initialises a new instance of the RuntimeException class.
	RuntimeErrorException::RuntimeErrorException(const char *format, ...)
	{
		va_list args;
		va_start(args, format);
		char buffer[256];
		vsprintf_s(buffer, 256, format, args);
		_message = buffer;
	}

	// Initialises a new instance of the SyntaxErrorException class.
	SyntaxErrorException::SyntaxErrorException(int line, int column, const char *source, const char *format, ...)
		: _line(line), _column(column), _source(source)
	{
		va_list args;
		va_start(args, format);
		char buffer[256];
		vsprintf_s(buffer, 256, format, args);
		_message = buffer;
	}

	// Gets the line index.
	int SyntaxErrorException::GetLine()
	{
		return _line;
	}

	// Gets the character index.
	int SyntaxErrorException::GetColumn()
	{
		return _column;
	}
	
	// Initialises a new instance of the FileNotFoundException class.
	FileNotFoundException::FileNotFoundException(const char *filename)
		: _filename(filename), Exception("File '%s' not found.", filename)
	{
	}
}